package bb.lanxing.lib.devices.utils;

import com.garmin.fit.MessageIndex;

public final class CRC {
    private static final int[] crc16_table = {0, 52225, 55297, 5120, 61441, 15360, 10240, 58369, 40961, 27648, 30720, 46081, 20480, 39937, 34817, 17408};

    public static final int get16(int i, byte b) {
        int[] iArr = crc16_table;
        int i2 = (((i >> 4) & MessageIndex.MASK) ^ iArr[i & 15]) ^ iArr[b & 15];
        return (((i2 >> 4) & MessageIndex.MASK) ^ iArr[i2 & 15]) ^ iArr[(b >> 4) & 15];
    }

    public static short getCrc16(byte[] bArr, int i, int i2) {
        int i3 = 0;
        for (int i4 = 0; i4 < i2; i4++) {
            int i5 = i + i4;
            if (i5 >= bArr.length) {
                break;
            }
            i3 = get16(i3, bArr[i5]);
        }
        return (short) i3;
    }

    public static short getCrc16(byte[] bArr) {
        return getCrc16(bArr, 0, bArr.length);
    }
}
